/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By 深圳中科鑫智科技有限公司
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.app.service.shop.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cereshop.app.dao.shop.CereShopVisitDAO;
import com.shop.cereshop.app.service.shop.CereShopVisitService;
import com.shop.cereshop.commons.domain.shop.CereShopVisit;
import com.shop.cereshop.commons.domain.shop.vo.CereUserShopVisitVo;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.poi.ImportExeclUtil;
import me.chanjar.weixin.common.util.DataUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.xmlbeans.impl.xb.xmlconfig.impl.QnameconfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.swing.MenuItemLayoutHelper;

import java.util.Date;
import java.util.List;

@Service
public class CereShopVisitServiceImpl extends ServiceImpl<CereShopVisitDAO,CereShopVisit> implements CereShopVisitService {

    @Autowired
    private CereShopVisitDAO cereShopVisitDAO;

    public CereShopVisit getTodayRecord(Long buyerUserId,Long shopId){
        LambdaQueryWrapper<CereShopVisit> q1 = new LambdaQueryWrapper<>();
        q1.eq(CereShopVisit::getBuyerUserId,buyerUserId);
        q1.eq(CereShopVisit::getShopId,shopId);
        q1.orderByDesc(CereShopVisit::getVisitTime);
        q1.last("limit 1");
        List<CereShopVisit> list = list(q1);
        if (CollectionUtil.isEmpty(list)){
            return null;
        }
        CereShopVisit shopVisit = list.get(0);
        boolean flag = DateUtils.isSameDay(DateUtil.parseDate(shopVisit.getVisitTime()), new Date());
        if (flag){
            return shopVisit;
        }
        return null;
    }

    @Override
    public void insert(CereShopVisit visit) throws CoBusinessException {
        cereShopVisitDAO.insert(visit);
    }

    @Override
    public void updateBuyerData(Long buyerUserId, Long id) throws CoBusinessException {
        cereShopVisitDAO.updateBuyerData(buyerUserId,id);
    }
}
